package com.spancer.contentcenter.block;

import com.baidubce.BceClientConfiguration;
import com.baidubce.auth.BceV1Signer;
import com.baidubce.auth.DefaultBceCredentials;
import com.baidubce.http.BceHttpClient;
import com.baidubce.http.HttpMethodName;
import com.baidubce.http.handler.BceErrorResponseHandler;
import com.baidubce.http.handler.BceJsonResponseHandler;
import com.baidubce.http.handler.HttpResponseHandler;
import com.baidubce.internal.InternalRequest;
import com.baidubce.internal.RestartableMultiByteArrayInputStream;
import com.baidubce.services.iothisk.model.ActiveResponse;
import com.baidubce.util.DateUtils;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.util.*;

//mvn安装
/*
<dependency>
<groupId>com.baidubce</groupId>
<artifactId>bce-java-sdk</artifactId>
<version>0.10.260</version>
</dependency>
*/

public class PTEvidence {

    public static void main(String[] args) throws Exception {
        String ACCESS_KEY_ID = "d0407da94aaa4898b95fc775aedd4460";             // 用户的Access Key ID
        String SECRET_ACCESS_KEY = "26b2dfe4a3744e7c842ccca6338c0608";         // 用户的Secret Access Key
        String Address = "T4Vt9yz4MFtE3c9N87DRFKLiejS3xoc38";
        String ENDPOINT = "https://xuper.baidu.com/";  // 用户自己指定的域名，参考说明文档
        BceClientConfiguration config = new BceClientConfiguration();
        config.setCredentials(new DefaultBceCredentials(ACCESS_KEY_ID, SECRET_ACCESS_KEY));
        config.setEndpoint(ENDPOINT);
        BceV1Signer signer = new BceV1Signer();
        BceHttpClient client = new BceHttpClient(config, signer);

        // 构造请求URI
        URI uri;
        try {
            uri = new URI(ENDPOINT + "/saas/openapi/evidence/normalpostevidence");
        } catch (URISyntaxException e) {
            System.out.println("Invalid URI: " + e.getMessage());
            return;
        }
        // 设置请求内容
        String body = "address=" + Address + "&content=22";
        byte[] contentBytes = body.getBytes(StandardCharsets.UTF_8);
        InternalRequest internalRequest = new InternalRequest(HttpMethodName.POST, uri);
        //设置请求头
        Date timestamp = new Date();
        System.out.println(DateUtils.formatAlternateIso8601Date(timestamp));
        Map<String, String> heaters = new HashMap<>();
        heaters.put("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
        heaters.put("x-bce-date", DateUtils.formatAlternateIso8601Date(timestamp));
        internalRequest.setHeaders(heaters);
        //设置参与鉴权的参数
        List<byte[]> byteArrayList = new ArrayList<>();
        byteArrayList.add(contentBytes);
        RestartableMultiByteArrayInputStream contentStream = new RestartableMultiByteArrayInputStream(byteArrayList, contentBytes.length);
        internalRequest.setContent(contentStream);

        Class<ActiveResponse> responseClass = ActiveResponse.class;
        HttpResponseHandler[] responseHandlers = new HttpResponseHandler[]{
                new BceErrorResponseHandler(),
                new BceJsonResponseHandler(),
                // 可以根据需要添加更多的响应处理器
        };

        //发送请求
        client.execute(internalRequest, responseClass, responseHandlers);
    }
}



